package com.baizhi.controller;

import com.baizhi.entity.Picture;
import com.baizhi.entity.Product;
import com.baizhi.entity.Result;
import com.baizhi.service.PictureService;
import com.baizhi.service.ProductService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("product")
public class ProductController {

    @Autowired
    private ProductService productService;
    @Autowired
    private PictureService pictureService;

    @RequestMapping("findAll")
    @ResponseBody
    public List<Product> findAll(){
        return  productService.findAll();
    }

    @RequestMapping("showAll")
    @ResponseBody
    public List<Product> showAll(String type){
        return productService.showAll(type);
    }

    @RequestMapping("findById")
    @ResponseBody
    public Product findById(String id){
        return productService.findById(id);
    }

    @RequestMapping("save")
    @ResponseBody
    public Result save(Product product, MultipartFile[] productimage, HttpServletRequest request){
        if("".equals(product.getCatalogid()))
            product.setCatalogid(null);

        Result result = null;
        try{
            String realPath = request.getSession().getServletContext().getRealPath("/back/image/picture");
            String uuid = UUID.randomUUID().toString();
            product.setId(uuid);
            productService.save(product);
            Picture picture = new Picture();
            for (MultipartFile multipartFile : productimage) {
                String uuid1 = UUID.randomUUID().toString();
                String extension = FilenameUtils.getExtension(multipartFile.getOriginalFilename());
                String urlname = uuid1+"."+extension;
                String path = "/back/image/picture/"+urlname;
                picture.setId(uuid1);
                picture.setPath(path);
                picture.setProductid(uuid);
                pictureService.save(picture);
                multipartFile.transferTo(new File(realPath,urlname));
            }
            result = new Result();
            result.setStatus(true);
        }catch (Exception e){
            e.printStackTrace();
            result = new Result();
            result.setMessage("添加成功");
        }
        return  result;
    }

    @RequestMapping("update")
    @ResponseBody
    public Result update(MultipartFile[] productimage,Product product,HttpServletRequest request){
        Result result = null;
        try{
            if(productimage.length!=0){
                String realPath = request.getSession().getServletContext().getRealPath("/back/image/picture");
                List<Picture> byId = pictureService.findById(product.getId());
                for (MultipartFile multipartFile : productimage) {
                    for (Picture picture : byId) {
                        System.out.println(multipartFile.getOriginalFilename()+"======="+picture.getPath());
                        String urlname=picture.getId()+"."+FilenameUtils.getExtension(multipartFile.getOriginalFilename());
                        multipartFile.transferTo(new File(realPath,urlname));
                    }
                }

            }
            productService.update(product);
        }catch (Exception e){
            e.printStackTrace();
            result = new Result();
            result.setMessage("更新失败");
        }
        return result;
    }

    @RequestMapping("delete")
    @ResponseBody
    public Result delete(String id,HttpServletRequest request){
        Result result = null;
        try{
            List<Picture> pictureList = pictureService.findById(id);
            for (Picture picture : pictureList) {
                String realPath = request.getSession().getServletContext().getRealPath(picture.getPath());
                File file = new File(realPath);
                file.delete();
                pictureService.delete(picture.getId());
            }
            productService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            result = new Result();
            result.setMessage("删除失败");
        }
        return result;
    }
}
